package com.cy.cy2025.util

import android.annotation.SuppressLint
import android.icu.text.SimpleDateFormat
import android.util.Log
import com.cy.cy2025.BuildConfig
import com.cy.cy2025.MyApplication.Companion.app
import java.io.File
import java.util.Date

object LogUtil {

    val isDebug = BuildConfig.DEBUG

    fun i(tag: String, msg: String) {
        if (isDebug) {
            Log.i(tag, msg)
        }
    }

    fun e(tag: String, msg: String) {
        if (isDebug) {
            Log.e(tag, msg)
        }
    }

    fun d(tag: String, msg: String) {
        if (isDebug) {
            Log.d(tag, msg)
        }
    }

    fun v(tag: String, msg: String) {
        if (isDebug) {
            Log.v(tag, msg)
        }
    }

    fun w(tag: String, msg: String) {
        if (isDebug) {
            Log.w(tag, msg)
        }
    }

    fun wtf(tag: String, msg: String) {
        if (isDebug) {
            Log.wtf(tag, msg)
        }
    }

    fun printStackTrace(e: Exception) {
        if (isDebug) {
            e.printStackTrace()
        }
    }

    @SuppressLint("SimpleDateFormat")
    fun s(tag: String, msg: String) {
        if (!isDebug) return
        i(tag, msg)
        app.getExternalFilesDir(null)?.path?.let {
            File("$it/logs").mkdirs()
            val data = Date()
            val day = SimpleDateFormat("yyyy-MM-dd").format(Date())
            val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(data)
            val file = File("$it/logs/log_$day.txt")
            //进程号
            val pid = android.os.Process.myPid()
            //线程号
            val tid = android.os.Process.myTid()
            //tag长度限制23个字符
            var tagStr = tag
            if (tagStr.length > 23) {
                tagStr = tag.substring(0, 23)
            } else if (tagStr.length < 23) {
                tagStr = tagStr.padEnd(23, ' ')
            }
            file.appendText("$time $pid-$tid tag:$tagStr          $msg\n")
        }

    }
}